package com.xjscrm.console.web.sop;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xjscrm.common.entity.MerchatChild;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.param.operatePlan.EffectuateGroupDto;
import com.xjscrm.console.param.sop.*;
import com.xjscrm.console.param.user.ChildMerchatDto;
import com.xjscrm.console.param.wwDevice.WwDeviceQuery;
import com.xjscrm.console.service.common.user.UserService;
import com.xjscrm.console.service.customer.childUser.ChildMerchatService;
import com.xjscrm.console.service.customer.sop.SopBatchService;
import com.xjscrm.console.service.customer.sop.SopService;
import com.xjscrm.console.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(value = "sop管理", tags = "sop管理")
@RestController
@RequestMapping(value = "/sop")
public class SopController {

    @Autowired
    private SopService sopService;

    @Autowired
    private UserService userService;


    @Autowired
    private ChildMerchatService childMerchatService;

    @Autowired
    private SopBatchService sopBatchService;

    @ApiOperation(value = "新增sop模版", notes = "新增sop模版", response = Boolean.class)
    @PostMapping("/addSopTemplate")
    @ApiOperationSupport(order = 1)
    public Object addSopTemplate(@RequestBody SopTemplateDto sopTemplateDto) throws Exception {
        sopService.addSopTemplate(sopTemplateDto);
        return true;
    }


    @ApiOperation(value = "修改sop模版", notes = "修改sop模版", response = Boolean.class)
    @PostMapping("/updateSopTemplate")
    @ApiOperationSupport(order = 2)
    public Object updateSopTemplate(@RequestBody SopTemplateDto sopTemplateDto) throws Exception {
        sopService.updateSopTemplate(sopTemplateDto);
        return true;
    }

    @ApiOperation(value = "查询sop模版列表", notes = "查询sop模版列表", response = SopTemplateVo.class)
    @PostMapping("/getSopTemplateList")
    @ApiOperationSupport(order = 3)
    public Object getSopTemplateList(@RequestBody SopTemplateQuery sopTemplateQuery) throws Exception {
        return sopService.getSopTemplateList(sopTemplateQuery);
    }


    @ApiOperation(value = "查询sop模版下拉", notes = "查询sop模版下拉", response = Map.class)
    @PostMapping("/getSopTemplateDropDown")
    @ApiOperationSupport(order = 4)
    public Object getSopTemplateDropDown(@RequestBody SopTemplateQuery sopTemplateQuery) throws Exception {
        return sopService.getSopTemplateDropDown(sopTemplateQuery);
    }


    @ApiOperation(value = "查询sop模版详情", notes = "查询sop模版详情", response = SopTemplateVo.class)
    @PostMapping("/getSopTemplateDetail")
    @ApiOperationSupport(order = 5)
    public Object getSopTemplateDetail(@RequestBody SopTemplateDto sopTemplateDto) throws Exception {
        return sopService.getSopTemplateDetail(sopTemplateDto);
    }

    @ApiOperation(value = "删除sop模版", notes = "删除sop模版", response = Boolean.class)
    @PostMapping("/delSopTemplate")
    @ApiOperationSupport(order = 6)
    public Object delSopTemplate(@RequestBody SopTemplateDto sopTemplateDto) throws Exception {
        sopService.delSopTemplate(sopTemplateDto);
        return true;
    }

    @ApiOperation(value = "创建sop任务", notes = "创建sop任务", response = Boolean.class)
    @PostMapping("/addSopTask")
    @ApiOperationSupport(order = 7)
    public Object addSopTask(@RequestBody SopTaskDto sopTaskDto) throws Exception {
        getUpdateUserId(sopTaskDto);
        sopService.addSopTask(sopTaskDto);
        return true;
    }


    @ApiOperation(value = "查询sop任务列表", notes = "查询sop任务列表", response = SopTaskVo.class)
    @PostMapping("/getSopTaskList")
    @ApiOperationSupport(order = 8)
    public Object getSopTaskList(@RequestBody SopTaskQuery sopTaskQuery) throws Exception {
        return sopService.getSopTaskList(sopTaskQuery);
    }


    @ApiOperation(value = "查询sop任务详情", notes = "查询sop任务详情", response = SopTaskVo.class)
    @PostMapping("/getSopTaskDetail")
    @ApiOperationSupport(order = 9)
    public Object getSopTaskDetail(@RequestBody SopTaskDto sopTaskDto) throws Exception {
        return sopService.getSopTaskDetail(sopTaskDto);
    }

    @ApiOperation(value = "删除sop任务", notes = "删除sop任务", response = Boolean.class)
    @PostMapping("/delSopTask")
    @ApiOperationSupport(order = 10)
    public Object delSopTask(@RequestBody SopTaskDto sopTaskDto) throws Exception {
        sopService.delSopTask(sopTaskDto);
        return true;
    }


    @ApiOperation(value = "关闭sop任务", notes = "关闭sop任务", response = Boolean.class)
    @PostMapping("/closeSopTask")
    @ApiOperationSupport(order = 11)
    public Object closeSopTask(@RequestBody CloseSopTaskDto closeSopTaskDto) throws Exception {
        sopService.closeSopTask(closeSopTaskDto);
        return true;
    }


    @ApiOperation(value = "获取sop机器人休眠列表", notes = "获取sop机器人休眠列表", response = SopTaskDeviceConfigVo.class)
    @PostMapping("/getSopTaskDeviceConfigList")
    @ApiOperationSupport(order = 12)
    public Object getSopTaskDeviceConfigList(@RequestBody WwDeviceQuery wwDeviceQuery) throws Exception {
        return sopService.getSopTaskDeviceConfigList(wwDeviceQuery);
    }

    @ApiOperation(value = "批量设置sop机器人休眠规则", notes = "批量设置sop机器人休眠规则", response = Boolean.class)
    @PostMapping("/batchUpdateSopTaskDeviceConfig")
    @ApiOperationSupport(order = 13)
    public Object batchUpdateSopTaskDeviceConfig(@RequestBody BathchUpdateSopTaskDeviceConfigDto bathchUpdateSopTaskDeviceConfigDto) throws Exception {
        sopBatchService.batchUpdateSopTaskDeviceConfig(bathchUpdateSopTaskDeviceConfigDto);
        return true;
    }


    @ApiOperation(value = "获取sop任务中心列表", notes = "获取sop任务中心列表", response = SopJobVo.class)
    @PostMapping("/getSopJobList")
    @ApiOperationSupport(order = 14)
    public Object getSopJobList(@RequestBody SopJobQuery sopJobQuery) throws Exception {
        return sopService.getSopJobList(sopJobQuery);
    }


    @ApiOperation(value = "关闭sop触发的任务", notes = "关闭sop触发的任务", response = Boolean.class)
    @PostMapping("/closeSopJob")
    @ApiOperationSupport(order = 15)
    public Object closeSopJob(@RequestBody SopJobDto SopJobDto) throws Exception {
        sopService.closeSopJob(SopJobDto);
        return true;
    }


    @ApiOperation(value = "获取生效群", notes = "获取生效群", response = EffectuateGroupVo.class)
    @PostMapping("/getEffectuateGroup")
    @ApiOperationSupport(order = 16)
    public Object getEffectuateGroup(@RequestBody EffectuateGroupDto effectuateGroupDto) throws Exception {
        return sopService.findAddFangetEffectuateGroupGroup(effectuateGroupDto);
    }





    public void getUpdateUserId(SopTaskDto sopTaskDto) {
        if (sopTaskDto.getOperatorType() != null && sopTaskDto.getOperatorType() == 0 && sopTaskDto.getMerId() != null) {
            sopTaskDto.setUpdaterId(sopTaskDto.getMerId());
        }

        if (sopTaskDto.getOperatorType() != null && sopTaskDto.getOperatorType() == 1 && sopTaskDto.getMerId() != null && sopTaskDto.getChildId() != null) {
            ChildMerchatDto childMerchatDto = new ChildMerchatDto();
            childMerchatDto.setMerId(sopTaskDto.getMerId());
            childMerchatDto.setId(sopTaskDto.getChildId());
            MerchatChild merchatChild = childMerchatService.getById(childMerchatDto);
            if (merchatChild == null) {
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该子账号不存在");
            }
            sopTaskDto.setUpdaterId(merchatChild.getId());
        }
    }
}
